using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace DataEntity
{
	public class T_Report_Deal : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlString		_report_Deal_MarketingRemark, _report_Deal_SZRemark, _report_Deal_DirecterRemark, _report_Deal_ManagerRemark;
			private SqlGuid			_report_Deal_ID, _report_Deal_MainID;
		#endregion


		public T_Report_Deal()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Report_Deal_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Deal_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Deal_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_MainID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_ManagerRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_ManagerRemark));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_MarketingRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_MarketingRemark));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_SZRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_SZRemark));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_DirecterRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_DirecterRemark));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Report_Deal::Insert::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Report_Deal_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Deal_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Deal_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_MainID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_ManagerRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_ManagerRemark));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_MarketingRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_MarketingRemark));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_SZRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_SZRemark));
				cmdToExecute.Parameters.Add(new SqlParameter("@sReport_Deal_DirecterRemark", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_DirecterRemark));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Report_Deal::Update::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Report_Deal_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Deal_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Report_Deal::Delete::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Report_Deal_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Report_Deal");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidReport_Deal_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _report_Deal_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_report_Deal_ID = (Guid)toReturn.Rows[0]["Report_Deal_ID"];
					_report_Deal_MainID = (Guid)toReturn.Rows[0]["Report_Deal_MainID"];
					_report_Deal_ManagerRemark = (string)toReturn.Rows[0]["Report_Deal_ManagerRemark"];
					_report_Deal_MarketingRemark = (string)toReturn.Rows[0]["Report_Deal_MarketingRemark"];
					_report_Deal_SZRemark = (string)toReturn.Rows[0]["Report_Deal_SZRemark"];
					_report_Deal_DirecterRemark = (string)toReturn.Rows[0]["Report_Deal_DirecterRemark"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Report_Deal::SelectOne::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Report_Deal_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Report_Deal");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Report_Deal::SelectAll::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlGuid Report_Deal_ID
		{
			get
			{
				return _report_Deal_ID;
			}
			set
			{
				_report_Deal_ID = value;
			}
		}


		public SqlGuid Report_Deal_MainID
		{
			get
			{
				return _report_Deal_MainID;
			}
			set
			{
				SqlGuid report_Deal_MainIDTmp = (SqlGuid)value;
				if(report_Deal_MainIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Report_Deal_MainID", "Report_Deal_MainID can't be NULL");
				}
				_report_Deal_MainID = value;
			}
		}


		public SqlString Report_Deal_ManagerRemark
		{
			get
			{
				return _report_Deal_ManagerRemark;
			}
			set
			{
				SqlString report_Deal_ManagerRemarkTmp = (SqlString)value;
				if(report_Deal_ManagerRemarkTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Report_Deal_ManagerRemark", "Report_Deal_ManagerRemark can't be NULL");
				}
				_report_Deal_ManagerRemark = value;
			}
		}


		public SqlString Report_Deal_MarketingRemark
		{
			get
			{
				return _report_Deal_MarketingRemark;
			}
			set
			{
				SqlString report_Deal_MarketingRemarkTmp = (SqlString)value;
				if(report_Deal_MarketingRemarkTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Report_Deal_MarketingRemark", "Report_Deal_MarketingRemark can't be NULL");
				}
				_report_Deal_MarketingRemark = value;
			}
		}


		public SqlString Report_Deal_SZRemark
		{
			get
			{
				return _report_Deal_SZRemark;
			}
			set
			{
				SqlString report_Deal_SZRemarkTmp = (SqlString)value;
				if(report_Deal_SZRemarkTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Report_Deal_SZRemark", "Report_Deal_SZRemark can't be NULL");
				}
				_report_Deal_SZRemark = value;
			}
		}


		public SqlString Report_Deal_DirecterRemark
		{
			get
			{
				return _report_Deal_DirecterRemark;
			}
			set
			{
				SqlString report_Deal_DirecterRemarkTmp = (SqlString)value;
				if(report_Deal_DirecterRemarkTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Report_Deal_DirecterRemark", "Report_Deal_DirecterRemark can't be NULL");
				}
				_report_Deal_DirecterRemark = value;
			}
		}
		#endregion
	}
}
